//
//  YSSearchArticleListCtrl.m
//  Yueshi
//
//  Created by yueshi－mac1 on 16/2/23.
//  Copyright © 2016年 悦食中国. All rights reserved.
//

#import "YSSearchArticleListCtrl.h"
#import "YSReviewsCtrl.h"
#import "YSSearchArticleCell.h"
#import "YSCategoryDataTool.h"

@interface YSSearchArticleListCtrl ()<UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *itemArray;
@property (assign, nonatomic) NSInteger pageIndex;
@property (copy, nonatomic) NSString *keywords;
@end

@implementation YSSearchArticleListCtrl

- (instancetype)initWithData:(NSArray *)array keywords:(NSString *)keywords{
    self = [super init];
    if (self) {
        [self.itemArray addObjectsFromArray:array];
        self.keywords = keywords;
    }
    return self;
}

- (NSMutableArray *)itemArray{
    if (!_itemArray) {
        _itemArray = [NSMutableArray array];
    }
    return _itemArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = self.keywords;
    
    [self initTableView];
    [self requestData];
}

- (void)initTableView{
    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
    header.lastUpdatedTimeLabel.hidden = YES;
    self.tableView.header = header;
    self.tableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
}

- (void)headerRefresh{
    self.pageIndex = 0;
    [self requestData];
}

- (void)footerRefresh{
    self.pageIndex ++;
    [self requestData];
}


- (void)requestData{
    [YSCategoryDataTool searchDataWithKeywords:self.keywords searchType:2 pageIndex:self.pageIndex success:^(YSSearchRes *result) {
         [UIUtil refreshComplete:self.tableView];
        if (result.isSucc) {
            if (self.pageIndex == 0) {
                [self.itemArray removeAllObjects];
            }
            
            [self.itemArray addObjectsFromArray:result.reviews];
            [self.tableView reloadData];
        }
    } failure:^(NSError *error) {
        [UIUtil refreshComplete:self.tableView];
        ShowTips(kRequestFailure);
    }];
}

#pragma - mark UITableViewDataSource,UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.itemArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YSSearchArticleCell *cell =  [YSSearchArticleCell cellWithTableView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setReview:self.itemArray[indexPath.row]];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return YSSearchArticleCellHeight;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    YSReviews *reviews =  self.itemArray[indexPath.row];
    YSReviewsCtrl *ctrl = [YSReviewsCtrl initWithReviews:reviews];
    [self.navigationController pushViewController:ctrl animated:YES];
}

@end
